<?php
class CscCache extends CCache {
    const SHARE_KEY = 'share.';
    public $servers;
    private $_cache;
    
    public function init()
    {
        parent::init();
        $this->_cache = new Memcache;
        foreach( $this->servers as $item ) {
            $this->_cache->addServer($item['host'],$item['port']);
        }
    }
    
    public function setShare($key,$value) {
        $key = $this->shareKey($key);
        $value = json_encode($value);
        return $this->_cache->set($key,$value,32,0);
    }
    
    public function getShare($key,$defaultValue=null) {
        $key = $this->shareKey($key);        
        $value = $this->_cache->get($key);
        $value = json_decode($value);
        return is_null($value)? $defaultValue : $value;
    }
    
    protected function getValue($key) {
        return $this->_cache->get($key);
    }
    
    protected function setValue($key,$value,$expire=0) {
        if ( !is_numeric($expire) || $expire<0 )
            $expire=0;
    
        return $this->_cache->set($key,$value,32,$expire);
    }
    
    protected function addValue($key,$value,$expire=0) {
        if ( !is_numeric($expire) || $expire<0 )
            $expire=0;
    
        return $this->_cache->add($key,$value,32,$expire);
    }
    
    protected function deleteValue($key) {
        return $this->_cache->delete($key);
    }
    
    protected function flushValues() {
        return $this->_cache->flush();
    }
    
    private function shareKey($key) {
        return self::SHARE_KEY . $key;
    }
}